package com.sycs.logindemo;

import com.sycs.logindemo.handler.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Arvin.Cao
 * @version 1.0
 * @time 16/10/13 下午2:37
 * @Description TODO
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    private final static String USERNAME = "sasasa";
    private final static String PASSWORD = "sasasa";

    /**
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity login(HttpSession session, String username, String password) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (username.equals(USERNAME) && password.equals(PASSWORD)) {
            session.setAttribute("user", "111111");
            return Response.success(data);
        } else {
            return Response.authorityFailed("用户名密码不正确");
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity get(HttpSession session) {
        Map<String, Object> data = new HashMap<String, Object>();
        String str = (String) session.getAttribute("user");
        if (str == null) {
            data.put("sucflag",false);
            data.put("message","当前用户未登录或已失效");
        }else{
            data.put("sucflag",true);
            data.put("message","获取用户信息");
            data.put("user","此处为用户数据(模拟测试)");
        }
        return Response.success(data);
    }
}
